package UI;

import Data.BasicData;
import Data.BitmapRequest;
import android.content.Context;
import android.content.Intent;
import android.graphics.Color;
import android.view.LayoutInflater;
import android.view.MenuItem;
import android.view.View;
import android.view.ViewGroup;
import android.widget.*;
import androidx.cardview.widget.CardView;
import tbs.littlebaby.EditItemActivity;
import tbs.littlebaby.ItemList;
import tbs.littlebaby.MainActivity;
import tbs.littlebaby.R;

import java.util.ArrayList;
import java.util.Calendar;

public class ExpandableListADP  extends BaseExpandableListAdapter{
    String[] group;
  public static   ArrayList<BasicData>[] childs;
    Context context;
    int cate;
    public ExpandableListADP(Context context,int cate,String[] Group,BasicData... datas)
    {
        this.cate=cate;
        this.context=context;
        group=Group;
        childs=new ArrayList[group.length];
        for(int i=0;i<childs.length;i++)
            childs[i]=new ArrayList<>();
        for(int i=0;i<datas.length;i++)
            childs[datas[i].getType()].add(datas[i]);
    }

    @Override
    public int getGroupCount() {
        return group.length;
    }

    @Override
    public int getChildrenCount(int groupPosition) {
        return childs[groupPosition].size();
    }

    @Override
    public Object getGroup(int groupPosition) {
        return childs[groupPosition];
    }

    @Override
    public Object getChild(int groupPosition, int childPosition) {
        return childs[groupPosition].get(childPosition);
    }

    @Override
    public long getGroupId(int groupPosition) {
        return groupPosition;
    }

    @Override
    public long getChildId(int groupPosition, int childPosition) {
        return groupPosition*10+childPosition;
    }

    @Override
    public boolean hasStableIds() {
        return false;
    }

    @Override
    public View getGroupView(int groupPosition, boolean isExpanded, View convertView, ViewGroup parent) {
        View view= LayoutInflater.from(context).inflate(R.layout.expandhead,null);
        view.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if(isExpanded)
                  ItemList.list.collapseGroup(groupPosition);
                else
                ItemList.list.expandGroup(groupPosition);
            }
        });
        TextView text=view.findViewById(R.id.header_title);
        text.setTextSize(16);
        text.setText(String.format("%s \n共有(%d)件物品",group[groupPosition],childs[groupPosition].size()));
        text.setOnLongClickListener(new View.OnLongClickListener() {
            @Override
            public boolean onLongClick(View v) {

                Toast.makeText(context, String.format("%s 代号:类别=%s  品类=%d",group[groupPosition],context.getString(cate),groupPosition),Toast.LENGTH_SHORT).show();
                return false;
            }
        });
        view.setPadding(2,5,2,5);
        if(isExpanded)
            text.setTextColor(Color.parseColor("#FFE91E63"));
        else
            text.setTextColor(Color.parseColor("#D56F2B"));

        Button bt=view.findViewById(R.id.header_add);
        bt.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Intent intent=new Intent(context, EditItemActivity.class);
                intent.putExtra("type",EditItemActivity.Add);
                intent.putExtra("pos",cate);
                intent.putExtra("child",groupPosition);
                context.startActivity(intent);
            }
        });
        return view;
    }
    public  int nowGroup,nowChild;

    void ChangeNowIndex(int g,int c)
    {
        nowGroup=g;
        nowChild=c;
    }


    @Override
    public View getChildView(int groupPosition, int childPosition, boolean isLastChild, View convertView, ViewGroup parent) {
        View view= LayoutInflater.from(context).inflate(R.layout.singlelistitem,null);
        PopupMenu menu= PopupMenuMaker.MakePopupMenu(context,view.findViewById(R.id.item_image));
        menu.getMenu().add("修改");
        menu.getMenu().add("删除");
        CardView cardView= view.findViewById(R.id.CARD);
        menu.setOnDismissListener(new PopupMenu.OnDismissListener() {
            @Override
            public void onDismiss(PopupMenu menu) {
                cardView.setCardBackgroundColor(Color.parseColor("#C5E1A5"));
            }
        });
        PopupMenuMaker.ApplyMenuItemCLick("修改", new IMenuSelectAction() {
            @Override
            public void Action(MenuItem item) {
                Edit(groupPosition,childPosition);
                menu.dismiss();
            }
        });
        PopupMenuMaker.ApplyMenuItemCLick("删除", new IMenuSelectAction() {
            @Override
            public void Action(MenuItem item) {
                DeleteButtonClicked(groupPosition,childPosition);
                menu.dismiss();
            }
        });
        view.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                cardView.setCardBackgroundColor(Color.parseColor("#FFFFF59D"));
                ChangeNowIndex(groupPosition,childPosition);
                menu.show();
            }
        });
        TextView pd= view.findViewById(R.id.item_priceDate);
        BasicData data=childs[groupPosition].get(childPosition);
        Calendar cal = Calendar.getInstance();
        String text;
        if(data.getDate()!=null)
        {
            cal.setTime(data.getDate());
           text =String.format("价格：%.2f元  \n购买日期：%d年%d月%d日",data.getPrice(),cal.get(Calendar.YEAR),cal.get(Calendar.MONTH)+1,cal.get(Calendar.DAY_OF_MONTH));
        }
        else
        {
            text =String.format("价格：%.2f元  \n购买日期：不详",data.getPrice());
        }

        pd.setText(text);
        pd=view.findViewById(R.id.item_tag);
        pd.setText(String.format("备注：%s",data.getTag()==null?"空备注":data.getTag()));
        TextView nb=view.findViewById(R.id.item_nameBrand);
        nb.setText(String.format("%s  品牌：%s",data.getName(),data.getBrand()));
        ImageView img=view.findViewById(R.id.item_image);
        img.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                ChangeNowIndex(groupPosition,childPosition);
                BitmapRequest.GetImagePathFromUser(context);
                ItemList.refreashList(groupPosition);
            }
        });
        if(data.getImage()!=null)
        {
            img.setImageBitmap(BitmapRequest.ScaleImage(data.getImage()));
        }
        return view;
    }

    void Edit(int g,int c)
    {
        Intent intent=new Intent(context, EditItemActivity.class);
        intent.putExtra("type",EditItemActivity.Edit);
        intent.putExtra("pos",cate);
        intent.putExtra("item",new int[]{g,c});
        context.startActivity(intent);
    }
    void DeleteButtonClicked(int group,int pos)
    {
        MainActivity.save.Delete(childs[group].get(pos).getID());
           childs[group].remove(pos);
           ItemList.refreashList(group);

    }

    @Override
    public boolean isChildSelectable(int groupPosition, int childPosition) {
        return false;
    }
}